<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Setting extends Controller_Admin_Main
{

    private $setting;

    public function before()
    {
   
        parent::before();
        Assets::js('jquery', $this->template->media_path . 'js/lhgdialog/lhgcore.lhgdialog.min.js');
        $this->setting = Model::factory('setting');
    }

    public function action_add()
    {   
        if ($_POST)
        {
            $data = array();
            $data['name'] = Arr::get($_POST, 'name', '');
            $data['parent_id'] = Arr::get($_POST, 'parent_id', 0);
            $data['code'] = Arr::get($_POST, 'code', '');
            $data['type'] = Arr::get($_POST, 'type', '');
            $data['file'] = Arr::get($_POST, 'file', '');
            $data['value'] = Arr::get($_POST, 'value', '');
            $data['position'] = Arr::get($_POST, 'position', 9999);

            if ($this->setting->is_exist(array('name' => $data['name']), 0, 'parent_id=' . $data['parent_id']))
            {
                Message::set(Message::ERROR, "\"" . $data['name'] . "\"" . '--配置名称已经存在');
                $this->request->redirect('admin/setting/add');
            }

            if ($this->setting->is_exist(array('code' => $data['code'])))
            {
                Message::set(Message::ERROR, "\"" . $data['code'] . "\"" . '--key已经存在');
                $this->request->redirect('admin/setting/add');
            }

            if ($this->setting->insert($data))
            {
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--配置添加成功');
                 $this->request->redirect('admin/setting/add');
            }
        }
        $this->template->assign('action', URL::site('admin/setting/add'));
        $this->template->assign('setting_group', $this->setting->get_setting_group());
        $this->template->view('setting_add');
    }

    public function action_list()
    {
        $id = intval($this->request->param('id'));
        $setting_group_name = $this->setting->get_var(array('id' => $id), 'name');
        $sub_setting = $this->setting->get_sub_setting($id);
        if ($_POST)
        {
            unset($_POST['dosubmit']);
            if (!$this->setting->update_sub_setting($_POST))
            {
                Message::set(Message::ERROR, '检查是否填的正确');
                $this->request->redirect('admin/setting/list/'. $id);
            }
            Message::set(Message::SUCCESS, '更新成功');
             $this->request->redirect('admin/setting/list/'. $id);
        }

        if (!count($sub_setting) > 0)
        {
            Message::set(Message::INFO, '暂无配置');
            // $this->request->redirect(Route::get('admin/setting')->uri(array('action' => 'list', 'id' => $id)));
        }
        $this->template->assign('sub_setting', $sub_setting);
        $this->template->assign('setting_group_name', $setting_group_name);
        $this->template->assign('action', URL::site('admin/setting/list/' . $id));
        $this->template->view('setting_list');
    }

    public function action_delete()
    {
        $id = intval($this->request->param('id'));

        if ($this->setting->is_exist(array('id' => $id)))
        {
            $setting = $this->setting->get_row(array('id' => $id), 'parent_id,name');
            if ($this->setting->delete(array('id' => $id)))
            {
                Message::set(Message::SUCCESS, $setting['name'] . '--删除成功');
            }
        }
        else
        {
            Message::set(Message::ERROR, '找不到要删除的配置');
            $this->request->redirect('admin/setting/list/id/2');
        }

        $this->request->redirect('admin/setting/list/id/'.$setting['parent_id']);
    }

    public function action_create_file()
    {
  
        $all_setting = $this->setting->get_all_setting();
        $file_path = SYSTEM_SETTING_CACHE_PATH . 'setting.php';
        if (Cache::instance()->write($file_path, $all_setting))
        {
            Message::success('写入成功');
            $this->request->redirect('admin/setting/add');
        }
    }

    public function action_left()
    {
        $setting_group_temp = $this->setting->get_setting_group();

        $setting_group = array();
        if (count($setting_group_temp) > 0)
        {
            $setting_group[0]['name'] = '添加配置';
            $setting_group[0]['sub'][] = array('name' => '添加配置', 'url' => URL::site() . 'admin/setting/add/', 'id' => 1000);
            $setting_group[0]['sub'][] = array('name' => '生成缓存文件', 'url' => URL::site() . 'admin/setting/create_file/', 'id' => 1002);


            foreach ($setting_group_temp as $key => $value)
            {
                if (count($value['sub']) > 0)
                {
                    $setting_group[$value['id']]['name'] = $value['name'];
                    foreach ($value['sub'] as $value2)
                    {
                        $setting_group[$value['id']]['sub'][] = array('id' => $value2['id'], 'name' => $value2['name'], 'url' => URL::site() . $value2['value'] . '/' . $value2['id']);
                    }
                }
            }
        }
        unset($setting_group_temp);
        $this->template->assign('left_name', '网站设置');
        $this->template->assign('left_arr', $setting_group);
        $this->template->view('left');
    }

}